package com.xbdun.web.validatebmp.frame;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;

import com.xbdun.web.validatebmp.frame.annotation.PayPathConfig;
import com.xbdun.web.validatebmp.frame.gateway.BaseWayPathConfig;

/**
* @ClassName: AnnotationBeanNameCfg
* @Description: 设置动态Bean名称
* @author zhanggt 360730440@qq.com
* @date 2017年5月19日 下午7:26:48
*/
public class AnnotationBeanNameCfg extends AnnotationBeanNameGenerator {

	@Override
	protected String buildDefaultBeanName(BeanDefinition definition) {
		
		PayPathConfig annotation;
		Class<?> forName = null;
		try {
			forName = Class.forName(definition.getBeanClassName());
			if(null!=forName){
				annotation = forName.getAnnotation(PayPathConfig.class);
				if(null!=annotation){
					Type sType = forName.getGenericSuperclass();
				    Type[] generics = ((ParameterizedType)sType).getActualTypeArguments();
				    annotation.value().setRqtClasses((Class<?>)generics[0]);
				    annotation.value().setMethod((annotation.value()+"").toLowerCase().replace("_","."));
				    //设置枚举对应方法路径
				    BaseWayPathConfig.putTradeBeanPath(annotation.value().getMethod(),annotation.value());
					return annotation.value().getMethod();
				}
			}
		
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}finally{
			//释放指针地址
			if(null!=forName){forName = null;}
		}
		
		return super.buildDefaultBeanName(definition);
	}
}
